package com.cyz.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//没有注入Spring容器中，监听不会生效
//@Component
public class ConsumerLister {

    @Autowired
    private RabbitTemplate rabbitTemplate;
//     监听消息队列
    @RabbitHandler
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("spring.directExchange"),
            key = "spring.directRouting",
            value = @Queue("spring.queue")))
    public void receiveMessage(Message message) {
        Object fromMessage = rabbitTemplate.getMessageConverter().fromMessage(message);
        System.out.println(fromMessage);
    }
}
